package com.sunseagear.wind.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sys.entity.DictGroup;
import com.sunseagear.wind.modules.sys.entity.query.DictGroupQuery;
import com.sunseagear.wind.modules.sys.service.IDictGroupService;
import com.sunseagear.wind.common.utils.DictUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/dict/group")
@Log(title = "字典分组")
@RequiredArgsConstructor
public class DictGroupController extends BaseBeanController {

    private final IDictGroupService dictGroupService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:dict:group:list")
    public RPage<DictGroup> list(DictGroupQuery query) {
        //加入条件
        QueryWrapper<DictGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(DictGroup::getCreateDate)
                .like(ObjectUtils.isNotEmpty(query.getKeyword()),DictGroup::getName, query.getKeyword())
                .or()
                .like(ObjectUtils.isNotEmpty(query.getKeyword()),DictGroup::getCode, query.getKeyword());
        // 预处理
        Page<DictGroup> pageBean = dictGroupService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 新增保存
     * @param entity 实体对象
     * @return 操作结果
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sys:dict:group:add")
    public R<Void> add(@Validated @RequestBody DictGroup entity) {
        dictGroupService.save(entity);
        DictUtils.initDict();
        return R.success("添加成功");
    }

    /**
     * 修改保存
     * @param entity 实体对象
     * @return 操作结果
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("sys:dict:group:update")
    public R<Void> update(@Validated @RequestBody DictGroup entity) {
        dictGroupService.saveOrUpdate(entity);
        DictUtils.initDict();
        return R.success("更新成功");
    }

    /**
     * 删除
     * @param idList 主键
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sys:dict:group:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        dictGroupService.removeBatchByIds(idList);
        DictUtils.initDict();
        return R.success("删除成功");
    }

}
